package com.example.bootcurriculumproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bootcurriculumproject.entity.Orders;
import com.example.bootcurriculumproject.mapper.OrderMapper;
import com.example.bootcurriculumproject.service.OrderService;
import com.example.bootcurriculumproject.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisUtil redisUtil;

    //查询订单时都得加上uid查询
    //查询所有订单
    @Override
    public List selectOrders() {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        String uid = String.valueOf(redisUtil.get("nowUid"));
        queryWrapper.eq(Orders::getNowuid,uid);
        return orderMapper.selectList(queryWrapper);
    }
    //删除相关用户所有订单(传入uid)
    public boolean deleteAll(String uid){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getNowuid,uid);
        this.remove(queryWrapper);
        return true;
    }
    //删除单条订单(传入订单号)
    public boolean deleteOne(String consignmentnum){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getConsignmentnum,consignmentnum);
        this.remove(queryWrapper);
      return true;
    }

    @Override
    public boolean updateStatus(String consignmentnum) {
        int state = orderMapper.selectStatus(consignmentnum);
        if(state==1){
            orderMapper.updateStatus(0,consignmentnum);
        }else {
            orderMapper.updateStatus(1,consignmentnum);
        }
        return true;
    }

    public List selectAboutState(int state){
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        String uid = String.valueOf(redisUtil.get("nowUid")) ;
        queryWrapper.eq(Orders::getNowuid,uid);
        queryWrapper.eq(Orders::getStates,state);
        return orderMapper.selectList(queryWrapper);
    }

    @Override
    public boolean updateAddress(int address, String consignmentnum) {
        orderMapper.updateAddressNum(address,consignmentnum);
        return true;
    }
}
